package 剑指Offer1;

public class _39平衡二叉树 {
    public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode(int x) { val = x; }
    }

    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        return Math.abs(height(root.left) - height(root.right)) <=1
                && isBalanced(root.left) && isBalanced(root.right);
    }

    //输入一个节点，返回它的高度
    public int height(TreeNode root){
        if(root == null) return 0;
        return Math.max(height(root.left) ,height(root.right)) + 1;
    }
}
